package com.xianyun.site.iblog.convertor;

import com.xianyun.common.core.utils.FormatUtils;
import com.xianyun.site.iblog.co.user.SystemUserCO;
import com.xianyun.site.iblog.domain.user.model.SystemUser;
import com.xianyun.site.iblog.dto.user.data.SystemUserDTO;
import com.xianyun.site.iblog.gatewayimpl.database.domainobject.SystemUserDO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 用户类型转换
 * @author mac_zyj
 */
public class SystemUserConvertor {

    public static SystemUser toEntity(SystemUserCO systemUserCo){
        SystemUser systemUser = new SystemUser();
        BeanUtils.copyProperties(systemUserCo, systemUser);
        return systemUser;
    }

    public static SystemUser toEntity(SystemUserDO systemUserDO){
        SystemUser systemUser = new SystemUser();
        BeanUtils.copyProperties(systemUserDO, systemUser);
        return systemUser;
    }

    public static SystemUserDO toDataObject(SystemUser userProfile){
        SystemUserDO userProfileDO = new SystemUserDO();
        BeanUtils.copyProperties(userProfile, userProfileDO);
        return userProfileDO;
    }
    public static SystemUserDTO toSystemUserDTO(SystemUserDO searchData){
        SystemUserDTO loginResultDTO = new SystemUserDTO();
        com.xianyun.common.core.utils.bean.BeanUtils.copyProperties(searchData,loginResultDTO);
        loginResultDTO.setId(searchData.getId().intValue());
        loginResultDTO.setUserId(searchData.getUserId().intValue());
        return loginResultDTO;
    }

    public static SystemUserDO toDataObjectForCreate(SystemUser userProfile){
        userProfile.setUserName(FormatUtils.initUserDefaultUserName());
        userProfile.setUserNickName(FormatUtils.initDefaultUserNickName());
        userProfile.setPassword(new BCryptPasswordEncoder().encode(userProfile.getPassword()));
        //TODO 后续做成根据注册方式生产 例如 基础注册为user前缀 支付宝注册为 zfb 按照业务做扩展
        userProfile.setUniquePath(("user"+userProfile.getUserName()).replace(" ",""));
        return toDataObject(userProfile);
    }

    public static SystemUserDO  toDataObjectForUpdate(SystemUser userProfile){
        return toDataObject(userProfile);
    }
}
